package com.running.base.run.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * User: wangyu08
 * Date: 22/11/2017
 * Time: 01:01
 */
public class StringUtil {

    public static List<String> string2List(String src, String separator) {
        if (StringUtils.isEmpty(src) || StringUtils.isEmpty(separator)) {
            return new ArrayList<>();
        }
        List<String> target = new ArrayList<>();
        for (String item : src.split(separator)) {
            target.add(item);
        }
        return target;
    }

    public static List<Integer> string2IntList(String src, String separator) {
        if (StringUtils.isEmpty(src) || StringUtils.isEmpty(separator)) {
            return new ArrayList<>();
        }
        List<Integer> target = new ArrayList<>();
        for (String item : src.split(separator)) {
            if (StringUtils.isEmpty(item)) {
                continue;
            }
            target.add(Integer.valueOf(item));
        }
        return target;
    }

    /**
     * 把list转为delimit分隔的字符串
     *
     * @param list
     * @param delimit
     * @return
     */
    public static String list2String(List<?> list, String delimit) {
        if (list.isEmpty()) {
            return "";
        }

        StringBuilder out = new StringBuilder();
        for (int i = 0, n = list.size(); i < n; i++) {
            Object obj = list.get(i);

            out.append(obj.toString() + delimit);
        }
        return out.substring(0, out.length() - delimit.length());
    }


    public static boolean isNull(String item) {
        if (StringUtils.isEmpty(item)
                || item.equals("null")
                || item.equals("undefined")) {
            return true;
        }
        return false;
    }

    public static boolean isStringListEmpty(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return true;
        }
        for (String item : list) {
            if (StringUtils.isEmpty(item)) {
                return true;
            }
        }
        return false;
    }

    public static String removeOssPrefix(String ossPath) {
        if (StringUtils.isEmpty(ossPath)) {
            return ossPath;
        }
        // 剔除oss://bucket前缀
        if (ossPath.startsWith("oss://")) {
            ossPath = ossPath.replaceAll("oss://[^/]+/", "");
        }
        return ossPath;
    }

    public static String getBucket(String ossPath) {
        if (StringUtils.isEmpty(ossPath)) {
            return ossPath;
        }
        // 剔除oss://前缀
        if (ossPath.startsWith("oss://")) {
            ossPath = ossPath.replace("oss://", "");
            ossPath = Arrays.asList(ossPath.split("/")).get(0);
        }
        return ossPath;
    }

    public static String getOssFileName(String ossPath) {
        if (StringUtils.isEmpty(ossPath)) {
            return ossPath;
        }

        if (ossPath.startsWith("oss://")) {
            ossPath = ossPath.replace("oss://", "");
            String[] ossSplits = ossPath.split("/");
            ossPath = Arrays.asList(ossSplits).get(ossSplits.length - 1);
        }

        return ossPath;
    }
}
